<?php


namespace App\Http\Controllers;


use App\Http\Requests\Login\LoginRequest;
use App\Services\Java\ApiService;
use App\Services\Login;
use D1M\Common\Exceptions\BusinessException;
use D1M\Common\Helpers\Result;
use Illuminate\Http\Request;

class LoginController
{

    /**
     * POS系统的那个登录，过期规则是，一定时间不操作就登录失效，需要重新登录，这个时间可配置  配合redis处理，调用接口时刷新登录时间
     * 然后不允许多点登录
     * jwt
     */


    /**
     * @store_id 门店IDs
     * @username 账号
     * @password 密码
     */
    public function login(LoginRequest $request,Login $service,ApiService $api)
    {
        //$params = $request->only(['store_id','username','password']);
        $params = $request->only(['store_id','name','password']);

        //调WH接口 确认是否符合登录条件
        $data = [
            'passWord'=>$params['password'],
            'loginName'=>$params['name'],
            'shopCode'=>$params['store_id'],
        ];

//        "loginName": "test","password": "elipjd6t","shopCode": "456"
        $users = $api->requestJava('/auth/open/index/admin-login',$data,'post');
        if(!$users){
            throw new BusinessException('未获取到用户信息',-1002);
        }

        $res = $service->login($users);
        $res['user'] = $users;
        return Result::success($res);
    }

    public function smsCode(Request $request,ApiService $api)
    {
        $data = $request->only(['phone','shopCode']);
        $res = $api->requestJava('/auth/open/index/send-yx-sms',$data,'post');
        return Result::success($res);
    }

    public function checkToken(Request $request,Login $service)
    {
        $res = $service->selfCheckToken($request);
        return Result::success($res);
    }

    public function refreshToken(Request $request,Login $service)
    {
        $res = $service->refershToken($request);
        return Result::success($res);
    }

    public function logout(Request $request,Login $service)
    {
        $res = $service->logout($request);
        return Result::successMsg($res);
    }

    public function userInfo(Request $request,Login $service)
    {
        $res = $service->me($request);
        return Result::success($res);
    }

}